/*     */ package com.yc.importdata;
/*     */ 
/*     */ import com.yc.dao.importdata.ImportDAO;
/*     */ import com.yc.dao.model.FileBO;
/*     */ import com.yc.service.FolderService;
/*     */ import com.yc.util.ListContentsBFO;
/*     */ import com.yc.util.UtilBtsm;
/*     */ import com.yc.web.importdata.ImportUtil;
/*     */ import java.io.File;
/*     */ import java.io.FileInputStream;
/*     */ import java.io.FileNotFoundException;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.PrintWriter;
/*     */ import java.net.SocketException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Properties;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServlet;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import org.apache.commons.fileupload.FileItem;
/*     */ import org.apache.commons.fileupload.FileUploadException;
/*     */ import org.apache.commons.fileupload.disk.DiskFileItemFactory;
/*     */ import org.apache.commons.fileupload.servlet.ServletFileUpload;
/*     */ import org.apache.commons.logging.Log;
/*     */ import org.apache.commons.logging.LogFactory;
/*     */ import org.apache.poi.hssf.usermodel.HSSFSheet;
/*     */ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/*     */ 
/*     */ public class ImportUploadSectionServlet extends HttpServlet
/*     */ {
/*  38 */   private static final Log log = LogFactory.getLog(ImportUploadSectionServlet.class);
/*     */ 
/*  40 */   private String propertiesFileName = "/btsm.properties";
/*  41 */   private String importFilePathForSection = null;
/*  42 */   private ImportDAO dao = new ImportDAO();
/*     */ 
/*     */   public ImportUploadSectionServlet()
/*     */   {
/*     */     try
/*     */     {
/*  49 */       init();
/*     */     }
/*     */     catch (ServletException e) {
/*  52 */       e.printStackTrace();
/*     */     }
/*  54 */     UtilBtsm.createFolder(this.importFilePathForSection);
/*     */   }
/*     */ 
/*     */   public ListContentsBFO getAllFiles(Map condition)
/*     */   {
/*  61 */     ListContentsBFO bfo = new ListContentsBFO();
/*  62 */     File d = new File(this.importFilePathForSection);
/*  63 */     File[] fl = d.listFiles();
/*  64 */     List list = new ArrayList();
/*  65 */     for (int i = 0; i < fl.length; i++) {
/*  66 */       File e = fl[i];
/*  67 */       if (e.isFile()) {
/*  68 */         FileBO fileBo = new FileBO();
/*  69 */         fileBo.setId(String.valueOf(i));
/*  70 */         fileBo.setFileName(e.getName());
/*  71 */         list.add(fileBo);
/*     */       }
/*     */     }
/*     */ 
/*  75 */     bfo.setData(list.toArray());
/*  76 */     bfo.setTotalSize(0);
/*  77 */     return bfo;
/*     */   }
/*     */ 
/*     */   public boolean deleteFile(String strFileName)
/*     */   {
/*  82 */     boolean flag = true;
/*     */     try {
/*  84 */       UtilBtsm.deleteFile(this.importFilePathForSection, strFileName);
/*     */     }
/*     */     catch (Exception e) {
/*  87 */       flag = false;
/*     */     }
/*  89 */     return flag;
/*     */   }
/*     */ 
/*     */   public void destroy()
/*     */   {
/*  98 */     super.destroy();
/*     */   }
/*     */ 
/*     */   public void doGet(HttpServletRequest request, HttpServletResponse response)
/*     */     throws ServletException, IOException
/*     */   {
/*     */   }
/*     */ 
/*     */   public void doPost(HttpServletRequest request, HttpServletResponse response)
/*     */     throws ServletException, IOException
/*     */   {
/* 131 */     log.info("开始上传文件");
/* 132 */     String strReturn = "{success:true,msg:'文件上传成功！'}";
/*     */     try
/*     */     {
/* 135 */       DiskFileItemFactory factory = new DiskFileItemFactory();
/* 136 */       ServletFileUpload sevletFileUpload = new ServletFileUpload(factory);
/* 137 */       UtilBtsm.createFolder(this.importFilePathForSection);
/* 138 */       sevletFileUpload.setHeaderEncoding("utf-8");
/* 139 */       List fileItems = sevletFileUpload.parseRequest(request);
/* 140 */       Iterator iter = fileItems.iterator();
/* 141 */       String fileName = null;
/*     */ 
/* 143 */       iter = fileItems.iterator();
/* 144 */       while (iter.hasNext())
/*     */       {
/* 146 */         FileItem item = (FileItem)iter.next();
/* 147 */         fileName = item.getName();
/* 148 */         if (!item.isFormField())
/*     */         {
/* 150 */           log.info("上传文件名是：[" + fileName + "]");
/* 151 */           if (fileName.lastIndexOf("\\") != -1)
/* 152 */             fileName = fileName.substring(fileName.lastIndexOf("\\"));
/* 153 */           File fullFile = new File(fileName);
/* 154 */           File savedFile = new File(this.importFilePathForSection, fullFile.getName());
/* 155 */           item.write(savedFile);
/*     */         }
/*     */ 
/*     */       }
/*     */ 
/*     */     }
/*     */     catch (FileUploadException eup)
/*     */     {
/* 163 */       eup.printStackTrace();
/* 164 */       log.error(eup);
/*     */     }
/*     */     catch (SocketException clietEx) {
/* 167 */       log.warn("客户取消上传");
/*     */     }
/*     */     catch (Exception e) {
/* 170 */       strReturn = "{success:false,msg:'文件上传失败！'}";
/* 171 */       e.printStackTrace();
/*     */     }
/* 173 */     log.info(strReturn);
/* 174 */     response.setContentType("text/html; charset=utf-8");
/* 175 */     response.getWriter().print(strReturn);
/*     */   }
/*     */ 
/*     */   public void init()
/*     */     throws ServletException
/*     */   {
/*     */     try
/*     */     {
/* 186 */       Properties p = new Properties();
/* 187 */       InputStream in = FolderService.class.getResourceAsStream(this.propertiesFileName);
/* 188 */       p.load(in);
/* 189 */       in.close();
/*     */ 
/* 191 */       if (p.containsKey("importFilePathForSection")) {
/* 192 */         this.importFilePathForSection = p.getProperty("importFilePathForSection");
/*     */       }
/* 194 */       log.info("importFilePath=" + this.importFilePathForSection);
/*     */     }
/*     */     catch (Exception e) {
/* 197 */       e.printStackTrace();
/*     */     }
/*     */   }
/*     */ 
/*     */   public boolean importDataToBase(String fileName) {
/* 202 */     File file = new File(this.importFilePathForSection, fileName);
/* 203 */     log.info("import file: " + file.getAbsolutePath());
/*     */ 
/* 205 */     HSSFWorkbook workBook = null;
/*     */     try {
/* 207 */       workBook = new HSSFWorkbook(new FileInputStream(file));
/*     */     } catch (FileNotFoundException e) {
/* 209 */       e.printStackTrace();
/*     */     } catch (IOException e) {
/* 211 */       e.printStackTrace();
/*     */     }
/* 213 */     int sheetCount = workBook.getNumberOfSheets();
/* 214 */     log.info("sheetCount = [" + sheetCount + "]");
/*     */ 
/* 216 */     for (int i = 0; i < sheetCount; i++)
/*     */     {
/* 218 */       String arySheetNames = workBook.getSheetName(i);
/* 219 */       log.info("arySheetNames[" + i + "] = [" + arySheetNames + "]");
/*     */     }
/*     */ 
/* 222 */     HSSFSheet sheet = workBook.getSheet("中继段");
/* 223 */     if (sheet != null)
/*     */     {
/* 225 */       log.info("导入【中继段】！");
/* 226 */       List list = ImportUtil.importForSectionTotalInfoData(sheet);
/* 227 */       this.dao.importForSectionTotalInfoData(list);
/*     */     }
/*     */ 
/* 230 */     sheet = workBook.getSheet("基本信息");
/* 231 */     if (sheet != null)
/*     */     {
/* 233 */       log.info("导入【基本信息】！");
/* 234 */       List list = ImportUtil.importForFiberInfoData(sheet);
/* 235 */       this.dao.importForFiberInfoData(list);
/*     */     }
/*     */ 
/* 238 */     sheet = workBook.getSheet("户外信息");
/* 239 */     if (sheet != null)
/*     */     {
/* 241 */       log.info("导入【户外信息】！");
/* 242 */       List list = ImportUtil.importForOutInfoData(sheet);
/* 243 */       this.dao.importForOutInfoData(list);
/*     */     }
/*     */ 
/* 247 */     sheet = workBook.getSheet("纤芯使用情况");
/* 248 */     if (sheet != null)
/*     */     {
/* 250 */       log.info("导入【纤芯使用情况】！");
/* 251 */       List list = ImportUtil.importFiberUsedInfoData(sheet);
/* 252 */       this.dao.importFiberUsedInfoData(list);
/*     */     }
/*     */ 
/* 255 */     sheet = workBook.getSheet("纤芯使用对照");
/* 256 */     if (sheet != null)
/*     */     {
/* 258 */       log.info("导入【纤芯使用对照】！");
/* 259 */       List list = ImportUtil.importUsedFiberMapData(sheet);
/* 260 */       this.dao.importUsedFiberMapData(list);
/*     */     }
/*     */ 
/* 263 */     return true;
/*     */   }
/*     */ }

/* Location:           E:\java\apache-tomcat-6.0.36\webapps\btsm\WEB-INF\classes\
 * Qualified Name:     com.yc.importdata.ImportUploadSectionServlet
 * JD-Core Version:    0.6.2
 */